<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="字典名称:">
        <el-select v-model="queryParams.dictType">
          <el-option placeholder="请选择字典名称"  v-for="item in typeOptions" :key="item.dictType" :label="item.dictName" :value="item.dictType" />
        </el-select>
      </el-form-item>
      <el-form-item label="显示值:">
        <el-input v-model="queryParams.dictLabel" placeholder="请输入字典显示值" />
      </el-form-item>
      <el-form-item label="状态:">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery('1')">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleQuery('reset')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" :disabled="single" icon="el-icon-edit" @click="handleUpdate(null)">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="danger" :disabled="multiple" icon="el-icon-delete" @click="handleDelete(null)">删除</el-button></el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column prop="dictLabel" label="显示值" min-width="80px" />
      <el-table-column prop="dictValue" label="实际值" />
      <el-table-column prop="orderNum" label="显示顺序" min-width="120px" />
      <el-table-column prop="status" label="状态" min-width="50px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="50px" />
      <el-table-column prop="createTime" label="创建日期" min-width="120px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="handleQuery" />
  
    <el-dialog :visible.sync="dialogVisible" :title="title" width="600px">
      <el-form ref="edit" :model="form" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示值">
              <el-input v-model="form.dictLabel" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际值">
              <el-input v-model="form.dictValue" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序">
              <el-input-number v-model="form.orderNum" :min="1" :max="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" :rows="4" v-model="form.remark" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { listDictType, addDictInfo, updateDictInfo, changeDictInfoStatus } from '@/api/system/dict'

export default {
  mixins: [initData],
  data() {
    return {
      statusOptions: [],
      queryParams: {
        dictLabel: undefined,
        dictType: this.$route.params.dictType,
        status: undefined
      },
      formLabelWidth: '80px',
      // 数据字典类型
      typeOptions: [],
      formReset: {
        status: '1'
      }
    }
  },
  mounted() {
    this.handleQuery()
    this.getDict('status_normal').then(res => {
      this.statusOptions = res.data
    })
    this.getDictTypeList()
  },
  methods: {
    beforeInit() {
      this.base = '/system/dict/info'
      this.modalName = '数据字典'
    },
    // 修改状态
    handleStatusChange(row) {
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '【' + row.nickname + '】用户吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeDictInfoStatus(row.id, row.status)
      }).then(res => {
        this.msgSuccess(text + '成功')
      }).catch(() => {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    handleAdd() {
      this.title = '新增' + this.modalName
      this.form = this.formReset
      this.dialogVisible = true
      this.form.dictType = this.queryParams.dictType
    },
    // 获取所有的数据字典类型
    getDictTypeList() {
      listDictType().then(res => {
        this.typeOptions = res.data
      })
    },
    submitForm() {
      this.$refs['edit'].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            addDictInfo(this.form)
          } else {
            updateDictInfo(this.form)
          }
          this.dialogVisible = false
          this.handleQuery()
        }
      })
    }
  }

}
</script>

<style>

</style>
